package com.tfjybj.itoo.teach.provider.service.impl;

import org.apache.dubbo.config.annotation.Reference;;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmsdbj.itoo.authorizationManagement.entity.AllusersEntity;
import com.dmsdbj.itoo.authorizationManagement.entity.OrganizationUserEntity;
import com.dmsdbj.itoo.authorizationManagement.facade.AllusersFacade;
import com.dmsdbj.itoo.authorizationManagement.facade.OrganizationUserFacade;
import com.dmsdbj.itoo.authorizationManagement.facade.RoleFacade;
import com.dmsdbj.itoo.authorizationManagement.facade.UserRoleFacade;
import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.dmsdbj.itoo.tool.uuid.BaseUuidUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.ClassEntity;
import com.tfjybj.itoo.teach.entity.ProfessionEntity;
import com.tfjybj.itoo.teach.entity.StudentEntity;
import com.tfjybj.itoo.teach.model.AcademyModel;
import com.tfjybj.itoo.teach.model.RoomModel;
import com.tfjybj.itoo.teach.model.StudentModel;
import com.tfjybj.itoo.teach.provider.dao.StudentDao;
import com.tfjybj.itoo.teach.provider.service.*;
import com.tfjybj.itoo.teach.template.StudentExportTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.jplus.hyberbin.excel.utils.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

import static com.tfjybj.itoo.teach.provider.until.DemoConstants.*;

//import com.tfjybj.itoo.teach.provider.dao.AcademyDao;
//import com.tfjybj.itoo.teach.provider.dao.ClassDao;
//import com.tfjybj.itoo.teach.provider.dao.ProfessionDao;

/**
 * StudentService接口实现类
 * ${base}表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Service
@org.apache.dubbo.config.annotation.Service
@Slf4j
public class StudentServiceImpl extends BaseServicePlusImpl<StudentDao, StudentEntity> implements StudentService {
	
	//region 模板生成
    @Resource
    private StudentDao studentDao;

	
    @Override
    public PageInfo<StudentEntity> queryByLikeName(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(studentDao.queryLikeName(name));
    }
	//endregion

    /* **********************************以下为非模板生成的内容********************************* */

    @Resource
    private RoomService roomService;
//    @Resource
//    private AcademyDao academyDao;
//    @Resource
//    private ProfessionDao professionDao;
//    @Resource
//    private ClassDao classDao;

    @Resource
    private AcademyService academyService;
    @Resource
    private ProfessionService professionService;
    @Resource
    private ClassService classService;
    @Resource
    private StudentService studentService;
//    @Resource
//    private AllusersFacade allusersFacade;
//    @Resource
//    private RoleFacade roleFacade;
//    @Resource
//    private UserRoleFacade userRoleFacade;
//    @Resource
//    private OrganizationUserFacade organizationUserFacade;
    @Resource
    private RedisTemplate<String, StudentExportTemplate> redisTemplate;

    @Override
    public PageInfo<StudentModel> queryLikeNameAndCode(String name, String code, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(studentDao.queryLikeNameAndCode(name.trim(),code.trim()));
    }

    @Override
    public List<StudentModel> queryStudentByIds(@Param("list") List<String> ids) {
        return new LinkedList<>(studentDao.selectListByStudentIds(ids));
    }

    @Override
    public PageInfo<StudentModel> queryStudentByByClassIds(@Param("list") List<String> ids,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(studentDao.selectListByClassIds(ids));
    }

    @Override
    public PageInfo<StudentModel> queryStudentByByProfessionIds(@Param("list") List<String> ids,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(studentDao.selectListByProfessionIds(ids));
    }

    @Override
    public PageInfo<StudentModel> queryStudentByAcademyIds(@Param("list") List<String> ids,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(studentDao.selectListByAcademyIds(ids));
    }

    @Override
    public PageInfo<StudentModel> queryLikeNameAndCodeAndClassId(String name,String code,String ClassId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(studentDao.queryLikeNameAndCodeAndClassId(name.trim(),code.trim(),ClassId.trim()));
    }

    @Override
    public PageInfo<StudentModel> queryStudentInfoByClassId (String classId,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(studentDao.selectByClassId(classId));
    }

    @Override
    public PageInfo<StudentModel> queryAllStudent (Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(studentDao.selectAllStudent());
    }

    @Override
    public PageInfo<StudentModel> queryStudentInfoByStudentId (String studentId) {
        return new PageInfo<>(studentDao.selectByStudentID(studentId));
    }

    @Override
    public PageInfo<StudentModel> queryStudentInfoByCodeAndNameStrLike (Integer pageNum, Integer pageSize,String strLike) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(studentDao.selectByCodeAndNameStrLike(strLike.trim()));
    }

    @Override
    public PageInfo<StudentModel> queryByCodeNameAndClassId (String strLike,String classId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(studentDao.selectByCodeNameAndClassId(strLike.trim(),classId));
    }

    @Override
    public List<StudentModel> queryStudentInfoByClassIds(@Param("list") List<String> ids) {
        //List<StudentModel> studentList=studentDao.selectListByClassId(ids);
        return studentDao.selectListByClassId(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addToAuth(StudentEntity student) {
        try {
//            //获得学生id
//            QueryWrapper<StudentEntity> studentEntityQueryWrapper = new QueryWrapper<>();
//
//            studentEntityQueryWrapper.eq("code", student.getCode());
//            List<StudentEntity> studentlist = studentService.list(studentEntityQueryWrapper);
//            String studentId = studentlist.get(0).getId();
//
//            // 调用权限接口，添加学生角色
//            AllusersEntity allusersEntity = new AllusersEntity();
//            allusersEntity.setId(studentId);
//            allusersEntity.setUserCode(student.getCode());
//            allusersEntity.setUserName(student.getName());
//            allusersEntity.setIsLock(0);
//            allusersEntity.setEmail(student.getEmail());
//            allusersEntity.setTelNum(student.getTelNum());
//            allusersEntity.setPassword(student.getCode());
//            allusersEntity.setSchoolNo("111222");// schoolNo
//            List<AllusersEntity> allusersEntityList = new ArrayList<>();
//            allusersEntityList.add(allusersEntity);
//            allusersFacade.insertUserList(allusersEntityList);
//
//            //插入角色关系表
//            String roleId = roleFacade.queryRoleId("学生", "lfsfxy111222"); //companyId
//            List<String> userIds = new ArrayList<>();
//            userIds.add(studentId);
//            userRoleFacade.insertUserRole(userIds, roleId);
//
//            //插入组织机构表
//            List<OrganizationUserEntity> organizationUserEntityList = new ArrayList<>();
//            OrganizationUserEntity organizationUserEntity = new OrganizationUserEntity();
//            organizationUserEntity.setOrganizationId(student.getClassesId());
//            organizationUserEntity.setUserId(studentId);
//            organizationUserEntity.setCompanyId("lfsfxy111222"); // companyId
//            organizationUserEntityList.add(organizationUserEntity);
//            organizationUserFacade.insertFromBasic(organizationUserEntityList);
        }
        catch (Exception e) {
            return false;
        }
        return true;
    }

  /**
     * 导出
     * @param response
     * @param strLike 关键词
     * @param studentCode
     * @return 导出excel
     * @author 郄子硕
     * @since 2018-11-26 12:04:30
     */

    @Override
    public ItooResult exportStudentInfo(HttpServletResponse response, String strLike, String studentCode, String studentName){
        strLike = strLike.trim();
        studentCode = studentCode.trim();
        studentName = studentName.trim();
        // 需要导出的数据
        List<StudentModel> studentInfo = new ArrayList<>();
        if(!StringUtils.isEmpty(strLike)){
            studentInfo = studentDao.selectByCodeAndNameStrLike(strLike);
        }else {
            studentInfo = studentDao.queryLikeNameAndCode(studentCode,studentName);
        }
        studentInfo =this.changeValue(studentInfo);

        // 定义excel字段
        Map<Serializable,Serializable> map=new HashMap<>();
        map.put(SHEET_NAME,"学生信息");
        //要导出的字段
        map.put(COLUMNS,new String[]{"code","name","sex","identityCardId","nation","entranceDate","academyName","professionName","className","roomName"});
        //导出表格的标题
        map.put(TITLE,"学生信息");
        map.put(NAME,"学生信息表");

        //数据导入到模板
        List<StudentExportTemplate> studentTemplates = new ArrayList<>();
        for (StudentModel studentModel: studentInfo) {
            StudentExportTemplate studentTemplate = new StudentExportTemplate();
            BeanUtils.copyProperties(studentModel,studentTemplate);
            studentTemplates.add(studentTemplate);
        }

        //不为空进行导出
        if (studentTemplates.size() == 0){
            return ItooResult.build(ItooResult.FAIL,"没有要导出的数据");
        }
        map.put(DATA_LIST,(Serializable) studentTemplates);
        //导出
        try {
            ExcelUtil.exportExcel(map,response);
        } catch (Exception e) {
            log.error("导出失败,未知的异常--"+e);
            return ItooResult.build(ItooResult.FAIL, "导出信息失败！");
        }
        return ItooResult.build(ItooResult.SUCCESS  , "导出信息成功！");
    }

    /**
     * 转换字段内容
     *
     * @param studentInfo 总集合
     * @return studentInfo
     * @author 郄子硕
     * @since 2018-11-27 11:03:07
     */
    private List<StudentModel> changeValue(List<StudentModel> studentInfo){
        //2、获得宿舍名称
        //2、1列出所需宿舍ID
        List<String> roomIds = new ArrayList<>();
        for(int i=0;i<studentInfo.size();i++){
            roomIds.add(studentInfo.get(i).getRoomId());
        }
        //2、2HashSet去重
        HashSet h = new HashSet(roomIds);
        roomIds.clear();
        roomIds.addAll(h);
        if (roomIds.size() >0){
            //2、3调接口查询房间名称
            List<RoomModel> roomList = roomService.queryRoomsbyIds(roomIds);
            //2、4 List转Map
            Map<String,String> roomMap = new HashMap<>();
            for(int i =0; i<roomList.size(); i++){
                roomMap.put(roomList.get(i).getId(),roomList.get(i).getName());
            }
            //2、4填充数据
            for (int i = 0 ; i < studentInfo.size() ; i++){
                String roomId = studentInfo.get(i).getRoomId();
                studentInfo.get(i).setRoomName(roomMap.get(roomId));
            }
        }
        return studentInfo;
    }


    @Override
    public List<StudentModel>  queryStudentByCode(String  code){
        return studentDao.queryStudentByCode( code);
    }

    /**
     * 下载模板-导入用
     *
     * @param response 响应
     * @return 下载成功或失败
     * @author 郄子硕
     * @since 2018-11-27 23:41:48
     */
    @Override
    public Boolean downTemplate(HttpServletResponse response){
        Map<Serializable,Serializable> map = new HashMap<>(16);
        List<StudentExportTemplate> templateList= new ArrayList<>();
        StudentExportTemplate studentTemplate = new StudentExportTemplate();
        studentTemplate.setCode("123456");
        studentTemplate.setName("张三");
        studentTemplate.setSex("男");
        studentTemplate.setIdentityCardId("130723200011074928");
        studentTemplate.setNation("汉族");
        studentTemplate.setEntranceDate("2018-08-08");
        studentTemplate.setAcademyName("数学与信息科学学院");
        studentTemplate.setProfessionName("信息与计算科学");
        studentTemplate.setClassName("18级信息与计算科学1班");
        templateList.add(studentTemplate);
        //excel设置
        map.put(SHEET_NAME,"学生信息");
        map.put(COLUMNS,new String[]{"code","name","sex","identityCardId","nation","entranceDate","academyName","professionName","className"});
        map.put(TITLE,"学生信息表");
        map.put(NAME,"学生信息模板");
        map.put(DATA_LIST,(Serializable) templateList);

        try {
            ExcelUtil.exportExcel(map,response);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 导入学生信息
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return itooresult 批量导入的结果
     * @author 郄子硕
     * @since 2018-11-28 16:39:56
     */
    @Override
    public ItooResult importStudent(MultipartFile multipartFile, HttpServletRequest request, HttpServletResponse response){
        response.setContentType("UTF-8");
        try {
            String fileName = multipartFile.getOriginalFilename();
            if (fileName == null) {
                log.error("导入失败，fileName is null");
                return  ItooResult.build(ItooResult.FAIL,"传入的文件为空");
            }
            // excel解析成list
            Map<Serializable,Serializable> map = new HashMap<>(4);
            map.put(SHEET_NAME, "学生信息");
            map.put(CLASS, StudentExportTemplate.class);
            List<StudentExportTemplate> studentImportList = ExcelUtil.importExcel(Objects.requireNonNull(fileName),map,request,response);
            if (StringUtils.isEmpty(studentImportList)) {
                return ItooResult.build(ItooResult.FAIL,"导入的数据为空");
            }
            //1.查询出所有学院数据
            List<AcademyModel> academyList = academyService.queryAcadOrInstInfo();
            //2.查询出所有专业数据
            List<ProfessionEntity> professionList = professionService.queryAllProfession();

            //3.查出所有班级数据
            List<ClassEntity> classyList = classService.queryAll();

            //5.将excel中的专业名称、班级名称等转为id
            List<StudentExportTemplate> studentList = new ArrayList<>();
            studentImportList.forEach(studentModel -> {
                StudentExportTemplate student = convertStudentInfo(studentModel, academyList, professionList, classyList);
                studentList.add(student);
            });

            // 定义导入错误数据集合
            List<StudentExportTemplate> errorStudentList = new ArrayList<>();
            String errorListId = BaseUuidUtils.base58Uuid();
            Long size = redisTemplate.opsForZSet().size(errorListId);
            size = size == null ? 0L : size;
            // 循环处理数据
            for (StudentExportTemplate excelStudent: studentList){
                // 数据验证
                if (!this.verify(excelStudent)) {
                    StudentExportTemplate studentExportTemplate = new StudentExportTemplate();
                    BeanUtils.copyProperties(excelStudent,studentExportTemplate);
                    errorStudentList.add(studentExportTemplate);
                    redisTemplate.opsForZSet().add(errorListId, studentExportTemplate, size + 1);
                    continue;
                }
//                // 获取学院id
//                String academyId = academyDao.queryLikeName(excelStudent.getAcademyName()).get(0).getId();
//                excelStudent.setAcademyId(academyId);
//                // 获取专业id
//                String professionId = professionDao.queryByName(excelStudent.getProfessionName()).get(0).getId();
//                excelStudent.setProfessionId(professionId);
//                // 获取班级id
//                String classesId = classDao.queryLikeName(excelStudent.getClassName()).get(0).getId();
//                excelStudent.setClassesId(classesId);
                StudentEntity studentEntity = new StudentEntity();
                BeanUtils.copyProperties(excelStudent,studentEntity);
                // 添加学生信息
                this.save(studentEntity);
                this.addToAuth(studentEntity);
            }

            // 不符合条件的信息返回
            if (errorStudentList.size()>0){
                return ItooResult.build("0001", "部分学生信息导入失败", errorListId);
            }
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL,"学生信息导入异常");
        }
        return ItooResult.build(ItooResult.SUCCESS,"学生信息导入成功");
    }


    /**
     * 导入学生信息
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return itooresult 批量导入的结果
     * @author 郄子硕
     * @since 2018-11-28 16:39:56
     */
    @Override
    public ItooResult importStudentTGB(MultipartFile multipartFile, HttpServletRequest request, HttpServletResponse response){
        response.setContentType("UTF-8");
        try {
            String fileName = multipartFile.getOriginalFilename();
            if (fileName == null) {
                log.error("导入失败，fileName is null");
                return  ItooResult.build(ItooResult.FAIL,"传入的文件为空");
            }
            // excel解析成list
            Map<Serializable,Serializable> map = new HashMap<>(4);
            map.put(SHEET_NAME, "学生信息");
            map.put(CLASS, StudentExportTemplate.class);
            List<StudentExportTemplate> studentImportList = ExcelUtil.importExcel(Objects.requireNonNull(fileName),map,request,response);
            if (StringUtils.isEmpty(studentImportList)) {
                return ItooResult.build(ItooResult.FAIL,"导入的数据为空");
            }
            //1.查询出所有学院数据
            List<AcademyModel> academyList = academyService.queryAcadOrInstInfo();
            //2.查询出所有专业数据
            List<ProfessionEntity> professionList = professionService.queryAllProfession();

            //3.查出所有班级数据
            List<ClassEntity> classyList = classService.queryAll();

            //5.将excel中的专业名称、班级名称等转为id
            List<StudentExportTemplate> studentList = new ArrayList<>();
            studentImportList.forEach(studentModel -> {
                StudentExportTemplate student = convertStudentInfo(studentModel, academyList, professionList, classyList);
                studentList.add(student);
            });

            // 定义导入错误数据集合
            List<StudentExportTemplate> errorStudentList = new ArrayList<>();
            String errorListId = BaseUuidUtils.base58Uuid();
            Long size = redisTemplate.opsForZSet().size(errorListId);
            size = size == null ? 0L : size;
            // 循环处理数据
            for (StudentExportTemplate excelStudent: studentList){
                // 数据验证
                if (!this.verify(excelStudent)) {
                    StudentExportTemplate studentExportTemplate = new StudentExportTemplate();
                    BeanUtils.copyProperties(excelStudent,studentExportTemplate);
                    errorStudentList.add(studentExportTemplate);
                    redisTemplate.opsForZSet().add(errorListId, studentExportTemplate, size + 1);
                    continue;
                }
//                // 获取学院id
//                String academyId = academyDao.queryLikeName(excelStudent.getAcademyName()).get(0).getId();
//                excelStudent.setAcademyId(academyId);
//                // 获取专业id
//                String professionId = professionDao.queryByName(excelStudent.getProfessionName()).get(0).getId();
//                excelStudent.setProfessionId(professionId);
//                // 获取班级id
//                String classesId = classDao.queryLikeName(excelStudent.getClassName()).get(0).getId();
//                excelStudent.setClassesId(classesId);
                StudentEntity studentEntity = new StudentEntity();
                BeanUtils.copyProperties(excelStudent,studentEntity);
                // 添加学生信息
                this.save(studentEntity);
//                this.addToAuth(studentEntity);
            }

            // 不符合条件的信息返回
            if (errorStudentList.size()>0){
                return ItooResult.build("0001", "部分学生信息导入失败", errorListId);
            }
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL,"学生信息导入异常");
        }
        return ItooResult.build(ItooResult.SUCCESS,"学生信息导入成功");
    }

    /**
     * 批量导入前验证数据是否符合要求
     *
     * @param
     * @return boolean
     * @author 郄子硕
     * @since 2018-11-28 16:48:17
     */
    private boolean verify(StudentExportTemplate studentTemplate) {
        // 判断非空字段是否有值
        if (StringUtils.isEmpty(studentTemplate.getCode())|| StringUtils.isEmpty(studentTemplate.getName())|| StringUtils.isEmpty(studentTemplate.getAcademyName())|| StringUtils.isEmpty(studentTemplate.getProfessionName())|| StringUtils.isEmpty(studentTemplate.getClassName())){
            studentTemplate.setFailReason("学号、姓名、学院、专业、班级等信息不能为空");
            log.warn("导入失败，学号、姓名、学院、专业、班级等信息不能为空，导入信息-",studentTemplate);
            return false;
        }
        // 判断学生学号是否重复
        if (studentDao.queryStudentByCode(studentTemplate.getCode()).size()>0) {
            studentTemplate.setFailReason("学号已存在");
            log.warn("导入失败，学号已存在，导入信息-",studentTemplate);
            return false;
        }
//        // 判断学院是否存在
//        if (academyDao.queryLikeName(studentTemplate.getAcademyName()).size() ==0) {
//            studentTemplate.setFailReason("学院不存在");
//            log.warn("导入失败，学院不存在，导入信息-",studentTemplate);
//            return false;
//        }
//        // 判断专业是否存在
//        if (professionDao.queryByName(studentTemplate.getProfessionName()).size()==0) {
//            studentTemplate.setFailReason("专业不存在");
//            log.warn("导入失败，专业不存在，导入信息-",studentTemplate);
//            return false;
//        }
//        // 判断班级是否存在
//        if (classDao.queryLikeName(studentTemplate.getClassName()).size()==0) {
//            studentTemplate.setFailReason("班级不存在");
//            log.warn("导入失败，班级不存在，导入信息-",studentTemplate);
//            return false;
//        }
        return true;
    }

    /**
     * 将导入excel中的学生实体转换为添加到数据库中的学生实体
     *
     * @param model    excel中的学生导入model
     * @param professionList 专业列表
     * @param classList      班级列表
     * @param academyList    学院列表
     * @return 学生实体
     */
    private StudentExportTemplate convertStudentInfo(StudentExportTemplate model, List<AcademyModel> academyList, List<ProfessionEntity> professionList, List<ClassEntity> classList) {
        StudentExportTemplate studentModel = new StudentExportTemplate();
        BeanUtils.copyProperties(model, studentModel);
        studentModel.setCode(model.getCode());
        //1.匹配专业id
        List<ProfessionEntity> professionCollect = professionList.stream().filter(e ->
                model.getProfessionName().equals(e.getName())
        ).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(professionCollect)) {
            studentModel.setProfessionId(professionCollect.get(0).getId());
        }
        //2.匹配班级id
        List<ClassEntity> classCollect = classList.stream().filter(e ->
                model.getClassName().equals(e.getName())
        ).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(classCollect)) {
            studentModel.setClassesId(classCollect.get(0).getId());
        }
        //3.匹配学院id
        List<AcademyModel> academyCollect = academyList.stream().filter(e ->
                studentModel.getAcademyName().equals(e.getName())
        ).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(academyCollect)) {
            studentModel.setAcademyId(academyCollect.get(0).getId());
        }
        return studentModel;
    }

    @Override
    public boolean downErrorList(String errorListId, HttpServletResponse response) {
        // redis 取出没有成功导入的人员信息
        Set set = redisTemplate.opsForZSet().range(errorListId, 0, -1);
        ArrayList studentTemplates = new ArrayList<>(set);
        // 清空redis数据
        redisTemplate.delete(errorListId);

        //导出
        if (studentTemplates.size() == 0) {
            return false;
        }
        Map<Serializable, Serializable> errorMap = new HashMap<>(16);
        errorMap.put(NAME, "学生信息未导入信息-" + System.currentTimeMillis());
        errorMap.put(SHEET_NAME, "学生信息");
        errorMap.put(TITLE, "未导入的信息");
        errorMap.put(COLUMNS,new String[]{"code","name","identityCardId","sex","nation","entranceDate","academyName","professionName","className","failReason"});
        errorMap.put(DATA_LIST, (Serializable) studentTemplates);
        try {
            ExcelUtil.exportExcel(errorMap, response);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public List<StudentModel> queryAll(String gradeId){
        return studentDao.queryAll( gradeId);
    }

}
